<template>
	<view class="content">
		<u-image width="100%" mode="widthFix" :src="imgUrl(sourceData.images)"></u-image>
		<view class="content-title over-one">
			<text> <text v-show="sourceData.hasOwnProperty('zhandi')">{{sourceData.zhandi}}㎡<text style="margin-left: 5rpx;margin-right: 5rpx;">·</text></text>{{sourceData.title}}
			</text>
		</view>
		<view class="content-price over-one">
			<text class="content-price-font">￥{{sourceData.price}}</text> <text class="content-price-small">元/㎡/月</text>
		</view>

		<view class="content-distance over-one" v-show="sourceData.distance != null">
			<text class="content-price-small">距离 {{sourceData.distance}} km</text>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			sourceDataProps: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				longitude: "",
				latitude: "",
				sourceData:{}
			}
		},
		created() {
			// console.log(this.sourceData)
           this.sourceData = this.sourceDataProps
		   this.getDistance(this.sourceData.longitude,this.sourceData.latitude)
		},
		methods: {
			getLocation() {
				let location = {}
				return new Promise((reserve, reject) => { //因为获取位置是异步接口所以需要使用promise
					uni.getLocation({
						success: (res) => {
							location.latitude = res.latitude
							location.longitude = res.longitude,
								reserve(location);
						},
						fail: (err) => {
							reserve({
								latitude: "",
								longitude: ""
							}); //获取失败则返回经纬坐标为0
						}
					})
				})
			},
			
			async getDistance(lng1,lat1) {
				if (this.longitude == "" && this.latitude == "") {
					let location = await this.getLocation()
					this.longitude = location.longitude
					this.latitude = location.latitude
				}
				if (this.longitude != ""  && this.latitude != "" ) {
					this.sourceData.distance = this.distance(lng1,lat1)
				}else{
					this.sourceData.distance = null
				}



			},
			distance(lng1,lat1) {
				let lat2 = this.latitude;    // 自身纬度
				let lng2 = this.longitude;   // 自身经度
				let rad1 = lat1 * Math.PI / 180.0;
				let rad2 = lat2 * Math.PI / 180.0;
				let a = rad1 - rad2;
				let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;

				let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(
					rad2) * Math.pow(
					Math.sin(b / 2), 2)));
				s = s * 6378.137;
				s = Math.round(s * 10000) / 10000;
				s = s.toString();
				s = s.substring(0, s.indexOf('.') + 2);
				// console.log('距离：', s);
				// console.log(s)
				return s; //返回距离
			}
		}
	}
</script>

<style scoped lang="less">
	.content {
		width: 100%;
		display: flex;
		flex-direction: column;

		.content-title {
			width: 100%;
			margin-top: 5rpx;
			font-size: 30rpx;
		}

		.content-price {
			width: 100%;
			margin-top: 10rpx;

			.content-price-font {
				margin-right: 10rpx;
				color: #DD524D;
				font-weight: bold;
				font-size: 28rpx;
			}

		}

		.content-price-small {
			font-size: 23rpx;
			color: #bdbdbd;
		}
	}
</style>
